<template>
    <div class="container">

        <el-card shadow="hover" ref="content_card">

            <!-- 基本搜索+功能按钮 -->
            <el-form class="mb10" ref="searchForm1" :inline="true" :model="searchForm" size="medium" label-width="">
                <el-card class="table_tool_bar_wrap" shadow="never">
                    <div class="base_wrap">
                        <div class="left">
                            <i class="el-icon-tickets" style="margin-right: 10px;"></i>
                            字典标签列表
                        </div>

                        <div class="right">
                            <el-button @click="handleAddOrUpdateVisible()" type="primary" size="medium" icon="el-icon-plus">新增</el-button>
                            <el-button @click="handleDelete()" type="danger" size="medium" icon="el-icon-delete">删除</el-button>
                        </div>
                    </div>
                </el-card>
            </el-form>

            <!-- 表格 -->
            <el-table
                ref="table"
                size="medium"
                empty-text="暂无数据"
                element-loading-text="给我一点时间"
                fit border highlight-current-row
                height="550"
                :stripe="stripe"
                v-loading="loadingData"
                :data="dataList"
                @selection-change="handleSelection">

                <el-table-column type="selection" width="55" prop="selection" v-if="isHideColumn('selection')" align="center"></el-table-column>
                <el-table-column label="标签" prop="label" v-if="isHideColumn('label')" align="left" show-overflow-tooltip/>
                <el-table-column label="字典值" prop="value" v-if="isHideColumn('value')" align="left" show-overflow-tooltip/>
                <el-table-column label="排序" prop="sort" v-if="isHideColumn('sort')" align="left" width="100" show-overflow-tooltip/>

                <el-table-column label="操作" prop="btn" v-if="isHideColumn('btn')" width="100px" align="left" fixed="right">
                    <template slot-scope="scope">
                        <el-button @click="handleAddOrUpdateVisible(scope.row.id)" type="text" size="small">编辑</el-button>
                        <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>

            <!-- 分页 -->
            <TablePagination
                :total.sync="total"
                :page.sync="searchForm.page"
                :limit.sync="searchForm.limit"
                @handleSizeChange="handleSizeChange"
                @handleCurrentChange="handleCurrentChange"/>
        </el-card>

        <!-- 弹窗, 新增 / 修改 -->
        <AddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></AddOrUpdate>

    </div>
</template>

<script>
import TableMixin from '@/mixin/TableMixin'
import TablePagination from '@/views/components/table/table-pagination'
import AddOrUpdate from './add-or-update-dialog'
export default {
    mixins: [TableMixin],
    components: {
        TablePagination,
        AddOrUpdate
    },

    data() {
        return {
            dataList: [],
            total: 0,
            loadingData: false,
            addOrUpdateVisible: false,
            selectionRows: [],
            searchFlag: true,
            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                keyword: null,
                dictId: null,
                label: null,
                value: null,
                selected: null,
                sort: null,
                status: null,
                remarks: null,
                ctime: null,
                utime: null
            },
            dictId: null
        }
    },

    created() {
    },

    mounted() {
        this.$nextTick(() => {
        })
    },

    methods: {
        /**
         * 新增或编辑
         */
        handleAddOrUpdateVisible(id) {
            // 表单窗口
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id, this.dictId)
            })
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/system/dict_detail/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList(this.searchForm.dictId)
                    }
                }
            }
        },

        /**
         * 获取表格数据
         */
        async getDataList(dictId) {
            this.loading = true
            this.searchForm.dictId = dictId
            this.dictId = dictId
            const res = await this.$http({
                url: '/system/dict_detail/page',
                method: 'GET',
                params: this.searchForm
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data.records
                this.total = res.data.total
            }
            this.loading = false
        },

        /**
         * 搜索
         */
        handleSearch() {
            this.getDataList()
        },
        /**
         * 重新加载数据表格
         */
        handleReload() {
            this.getDataList()
        },
        /**
         * 重置搜索表单
         */
        handleReset() {
            this.$refs.searchForm1.resetFields()
            this.$refs.searchForm2.resetFields()
        }

    }

}
</script>

<style lang="scss" scoped>

</style>
